import React from 'react';
import { Table } from 'antd';
import { MinusOutlined, PlusOutlined } from '@ant-design/icons';

const getRows = (data, parentKey = null) => {
  if (Array.isArray(data)) {
    const uniqueKeys = new Set(); // 用于存储已经使用过的键
    return data.map((item, index) => {
      let key = parentKey ? `${parentKey}-${index}` : `${index}`;
      if (uniqueKeys.has(key)) {
        key = `${key}-${Math.random().toString(36).substr(2, 5)}`; // 如果键已存在，则在末尾添加随机字符串
      }
      uniqueKeys.add(key);

      const row = {
        ...item,
        key,
      };
      if (Array.isArray(row.children) && row.children.length) {
        row.children = getRows(row.children, key);
      }
      return row;
    });
  }
};

const TreeTable = ({ data, columns, ...restProps }) => {
  const rows = getRows(data);

  return <Table dataSource={rows} pagination={false} columns={columns} {...restProps} />;
};

export default TreeTable;
